package com.qf.filter;

import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * 前台用户自动登录的过滤器
 *
 * 过滤路径:/jsp/login.jsp
 * 过滤规则:
 *      默认的请求规则:直接访问/jsp/login.jsp---直接过滤
 *      或者是通过后端Servlet完成的请求转发到/jsp/login.jsp ---直接过滤
 *      DispatchTypes
 */
@WebFilter(value = "/jsp/login.jsp",dispatcherTypes ={DispatcherType.REQUEST,DispatcherType.FORWARD} )
public class UserAutoLoginFilter implements Filter {
    //过滤器初始化
    public void init(FilterConfig config) throws ServletException {
        System.out.println("UserAutoLoginFilter初始化了");
    }

    //过滤器销毁
    public void destroy() {
        System.out.println("UserAutoLoginFilter销毁了");
    }

    /**
     * 过滤/jsp/login.jsp的过滤任务
     * @param req   请求对象
     * @param resp   响应对象
     * @param chain  过滤链
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doFilter(ServletRequest req,
                         ServletResponse resp,
                         FilterChain chain) throws ServletException, IOException {

        //1)将req和resp两个类型--转换HttpServletRequest /HttpServletResponse
        HttpServletRequest request = (HttpServletRequest) req ;
        HttpServletResponse response = (HttpServletResponse) resp ;

        //2)在直接访问/jsp/login.jsp或者请求转发访问/jsp/login.jsp,从请求对象中获取所有Cookie
        Cookie[] cookies = request.getCookies();
        //判断
        if(cookies!=null){
            //有cookie内容存在
            //遍历cookie的内容

            //定义字符串cookie内容
            String content = null ;
            for (Cookie cookie : cookies) {
                //判断获取的cookie名称是否为 "autoUser"
                if("autoUser".equals(cookie.getName())){
                    //获取"autoUser"cookie的名称对应的内容
                    content = cookie.getValue() ;
                }
            }

            if(content!=null){
                //存在
                //获取内容
                //需要解码--->jdk提供的工具 URLDecoder
                content = URLDecoder.decode(content,"utf-8") ;
                //content内容 "用户名=密码"
                String[] strs = content.split("=");
                //测试
                System.out.println(strs[0]+"---"+strs[1]);
                //strs[0] 用户名
                //strs[1] 密码
                //此时调用 用户的业务接口
                UserService userService = new UserServiceImpl() ;
                User user = userService.userLogin(strs[0], strs[1]);
                if(user!=null){
                    //获取到了
                    //获取HttpSession
                    HttpSession session = request.getSession();
                    session.setAttribute("user",user);
                    //重定向前台主页
                    response.sendRedirect(request.getContextPath()+"/");
                }else{
                    //放行
                    chain.doFilter(request,response);
                }


            }else{
                //不存在,没有内容
                //放行
                chain.doFilter(request,response);
            }
        }else{
            //放行
            chain.doFilter(request,response);
        }

    }
}
